<template>
  <div class="imgs">
    <div class="login-container">
      <h1>管理端后台</h1>
    <el-form
      ref="loginForm"
      :rules="rules"
      :model="loginForm"
      label-width="80px"
      class="login-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="loginForm.username"
          autocomplete="off"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
 
</template>

<script>
import { denglu } from "@/apis/lihan/Index.js";
export default {
  name: "LoginPage",
  data() {
    var validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 登录逻辑
          denglu(this.loginForm.username, this.loginForm.password).then(
            (ok) => {
              if (ok.data.code == 200) {
                this.$router.replace("/home");
              }else if(ok.data.code == 201){
                alert("账号或密码错误")
              }
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  height: 100%;
}
.login-form {
  width: 400px;
}
.imgs{
  width: 100%;
  height: 100%;
  background: url('https://video-js.51miz.com/preview/element/00/01/25/39/E-1253991-B53EE8B4.gif') no-repeat center;
  background-clip: border-box;
  background-size: 100%;
}
h1{
  height: 120px;
  color: orange;
}
</style>